﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.IO;
public partial class Newdir : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] == null)
            {

                Response.Redirect("Default.aspx");

            }

        }
    }
    protected void Btture_Click(object sender, EventArgs e)
    {
        string name = Session["admin"].ToString();
        string path = Server.UrlDecode(Request.QueryString["path"].ToString());
        string isq = Request.QueryString["q"].ToString();

        if (isq == "aa")
        {
            if (Txtdirname.Text == string.Empty)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('名字不能为空');</script>");
            }
            else
            {
                if (new DirInfoManager().IsExists(name, 0, Txtdirname.Text.ToString()))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('名字已经存在请重新输入');</script>");
                }
                else
                {
                    string newpath =Server .MapPath ("Upfile"+"\\"+ path +"\\"+ Txtdirname.Text.ToString());
                    
                    DirectoryInfo di = new DirectoryInfo(newpath );
                    di.Create();

                    bool b = new DirInfoManager().Insert(name, 0, Txtdirname.Text.ToString(), di.CreationTime.ToString ());
                    if (b)
                    {
                        Response.Redirect("Manager.aspx?q=" + isq + "&p=0");

                    }
                }
            }
        }
        else
        {
            int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());
            string dirname1 = Server.UrlDecode(Request.QueryString["dd"].ToString());
            int parentid = new DirInfoManager().FindId(name, myparentid, dirname1);
            if (Txtdirname.Text == string.Empty)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('名字不能为空');</script>");
            }
            else
            {
                if (new DirInfoManager().IsExists(name, parentid, Txtdirname.Text.ToString()))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('名字已经存在请重新输入');</script>");
                }
                else
                {
                    string newpath = Server.MapPath("Upfile" + "\\" + path + "\\" + Txtdirname.Text.ToString());

                    DirectoryInfo di = new DirectoryInfo(newpath);
                    di.Create();

                    bool b = new DirInfoManager().Insert(name, parentid, Txtdirname.Text.ToString(), di.CreationTime.ToString());
                    if (b)
                    {
                        string dirname = Server.UrlEncode(dirname1);
                        Response.Redirect("Manager.aspx?q=" + isq + "&dd=" + dirname + "&p=" + myparentid);

                    }
                }
            }

        }
    }
}
